package com.qh.book.service.impl;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.qh.book.converter.OrderMaster2OrderDTO;
import com.qh.book.dataobj.Book;
import com.qh.book.dataobj.Logistics;
import com.qh.book.dataobj.OrderDetail;
import com.qh.book.dataobj.OrderMaster;
import com.qh.book.dto.CartDTO;
import com.qh.book.dto.OrderDTO;
import com.qh.book.enums.LogisticsStatusEnum;
import com.qh.book.enums.OrderStatusEnum;
import com.qh.book.enums.PrimaryKeyEnum;
import com.qh.book.enums.ResultEnum;
import com.qh.book.exception.BsException;
import com.qh.book.repository.OrderDetailRepository;
import com.qh.book.repository.OrderMasterRepository;
import com.qh.book.service.BookService;
import com.qh.book.service.OrderMasterService;
import com.qh.book.utils.KeyUtil;
/**
 * 
 * @author qihong
 * @date 2017年9月16日 下午9:06:06 
 * @Description: TODO
 */


@Service
@Transactional
public class OrderMasterServiceImpl implements OrderMasterService {

	@Autowired
	private OrderMasterRepository orderMasterRepository;
	
	@Autowired
	private OrderDetailRepository orderDetailRepository;
	
	@Autowired
	private BookService bookService;
	
	/***
	 * 创建订单
	 * orderDTO里面只有bookId和
	 */
	@Override
	public String create(OrderDTO orderDTO) {
		//订单Id
		String orderId = KeyUtil.uniqueKey(PrimaryKeyEnum.ORDER_PRIMARYKEY.getNumber());
		List<CartDTO> cartDTOList = new ArrayList<CartDTO>();
		
		//添加一条物流信息
		Logistics logistics = new Logistics();
		logistics.setOrderId(orderId);
		logistics.setCurrentStatus(LogisticsStatusEnum.PENDING.getCode());
		
		BigDecimal orderAmount = new BigDecimal(BigInteger.ZERO);
		for (OrderDetail orderDetail : orderDTO.getOrderDetailList()) {
			Book book = bookService.findOne(orderDetail.getBookId());
			if(book == null) {
				throw new BsException(ResultEnum.BOOK_NOT_EXIST);
			}

			//计算总价
			orderAmount = book.getBookPrice().multiply(new BigDecimal(orderDetail.getBookQuantity())).add(orderAmount);
			orderDetail.setOrderId(orderId);
			orderDetail.setDetailId(KeyUtil.uniqueKey(PrimaryKeyEnum.ORDERDETAIL_PRIMARYKEY.getNumber()));
			orderDetail.setBookIcon(book.getBookIcon());
			//bookId在orderDetail已经有了
			//orderDetail.setBookId(bookId);
			orderDetail.setBookIsbn(book.getBookId());
			orderDetail.setBookName(book.getBookName());
			orderDetail.setBookPress(book.getBookPress());
			orderDetail.setBookPrice(book.getBookPrice());

			//详情入库
			orderDetailRepository.save(orderDetail);
			//便于减库存
			cartDTOList.add(new CartDTO(book.getBookId(),orderDetail.getBookQuantity()));
		}
		//构造主订单
		OrderMaster newOrder = new OrderMaster();
		newOrder.setOrderId(orderId);
		newOrder.setBuyerAddress(orderDTO.getBuyerAddress());
		newOrder.setBuyerName(orderDTO.getBuyerName());
		newOrder.setBuyerPhone(orderDTO.getBuyerPhone());
		newOrder.setOrderStatus(OrderStatusEnum.NEW.getCode());
		newOrder.setOrderAmount(orderAmount);
		orderMasterRepository.save(newOrder);
		
		//减库存
		bookService.decreaseStock(cartDTOList);
		return orderId;
	}

	@Override
	public OrderDTO findOne(String orderId) {
		OrderMaster order = new OrderMaster();
		order = orderMasterRepository.findOne(orderId);
		if(order == null) {
			throw new BsException(ResultEnum.ORDER_NOT_EXIST);
		}
		List<OrderDetail> orderDetailList = orderDetailRepository.findByOrderId(orderId);
		OrderDTO orderDTO = new OrderDTO();
		orderDTO = OrderMaster2OrderDTO.convert(order);
		orderDTO.setOrderDetailList(orderDetailList);
		return orderDTO;
	}

	@Override
	public Page<OrderDTO> findList(String buyerOpenid, Pageable pageable) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Page<OrderMaster> findList(Pageable pageable) {
		Page<OrderMaster> OrderMasterPage = orderMasterRepository.findAll(pageable);		
		return OrderMasterPage;
	}

	@Override
	public String cancel(String orderId) {
		//判断
		//归还库存
		//改订单状态
		//如果已支付  退款
		if(orderId == null||"".equals(orderId)) {
			throw new BsException(ResultEnum.ORDER_NOT_EXIST);
		}
		OrderMaster order = orderMasterRepository.findOne(orderId);
		if(order.getOrderStatus().equals(OrderStatusEnum.CANCEL.getCode())) {
			throw new BsException(ResultEnum.ORDER_CANCEL_FAIL);
		}
		List<OrderDetail> orderList = orderDetailRepository.findByOrderId(orderId);
		List<CartDTO> cartDTOList = new ArrayList<CartDTO>();
		for (OrderDetail orderDetail : orderList) {
			cartDTOList.add(new CartDTO(orderDetail.getBookId(),orderDetail.getBookQuantity()));
		}
		bookService.increaseStock(cartDTOList);
		if(order.getOrderStatus().equals(OrderStatusEnum.FINISHED.getCode())) {
			//TODO 退款
		}
		return orderId;
	}


	@Override
	public String paid(String orderId) {
		if(orderId == null||"".equals(orderId)) {
			throw new BsException(ResultEnum.ORDER_NOT_EXIST);
		}
		OrderMaster order = orderMasterRepository.findOne(orderId);
		if(!order.getOrderStatus().equals(OrderStatusEnum.NEW.getCode())) {
			throw new BsException(ResultEnum.ORDER_STATUS_ERROR);
		}
		// TODO 付款
		return null;
	}

}
